#!/bin/bash
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# r42.hsc src/hsc/scripts/getRemoteFrameFiles.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
#-------------------------------------------------------------------------------
# FTPs files from the HMC efix ftp server
#
# Possible return codes are:
#
# 0 - no error
# 1 - usage error, invalid argument
# 2 - invalid userID/password combination
# 3 - source file not found on server
# 4 - 'ftp' connection error
# 5 - "other" 'ftp' error
# 6 - target file not written to HMC
#
#-------------------------------------------------------------------------------
#
#set -x
 
function exit_cleanup {
#   rm -f $FTP_LOG_FILE
   chmod 777 $FTP_LOG_FILE
         
   # remove the file if successful download, else no issue with this cmd error
   rm -f /usr/local/hsc_install.images/$FILE
         
   exit $1
}

function ftpRemoteFiles {
   FTP_SERVER=$1
   REMOTE_FILE=$2
   REMOTE_DIR=`/usr/bin/dirname ${REMOTE_FILE}`
   FILE=`basename ${REMOTE_FILE}`
   USER=$3
   PASS=$4
   # remove it first
   if [ -d /usr/local/hsc_install.images/ ]
   then
      rm -rf /usr/local/hsc_install.images/*
   fi
   cat << END_CLIENT_FTP | /usr/kerberos/bin/ftp -n -u -v $FTP_SERVER 2>&1 > $FTP_LOG_FILE
user $USER $PASS
bin
cd $REMOTE_DIR
get $FILE /usr/local/hsc_install.images/$FILE
quit
END_CLIENT_FTP
} # end function getRemoteFiles

#-------------------------------------------------------------------------------
# "Main" program start   
#-------------------------------------------------------------------------------
FTP_LOG_FILE=/tmp/ftpFrame.log

if [ "$1" = "" ]
then
   echo "Usage: getRemoteFrameFiles <ftp server> <remote file> <user> <password>"
   exit_cleanup 1
fi

#-------------------------------------------------------------------------------
# 'ftp' the file from service server
#-------------------------------------------------------------------------------

LANG=en_US
export LANG

ftpRemoteFiles $1 $2 $3 $4 

TRANSFER_RESULTS=$(grep -i "Transfer complete" $FTP_LOG_FILE)
if [ -z "$TRANSFER_RESULTS" ]
then
   # ftp failed for some reason - find out why
   errRC=$(grep -i "USER and PASS" $FTP_LOG_FILE)
   if [ -n "$errRC" ]
   then
      exit_cleanup 2
   fi
   
   errRC=$(grep -i "cannot find the file" $FTP_LOG_FILE)
   if [ -n "$errRC" ]
   then
      exit_cleanup 3
   fi

   errRC=$(grep -i "No such file or directory" $FTP_LOG_FILE)
   if [ -n "$errRC" ]
   then
      exit_cleanup 3
   fi
   
   errRC=$(grep -i "Not connected" $FTP_LOG_FILE)
   if [ -n "$errRC" ]
   then
      exit_cleanup 4
   else
      # none of the above errors
      exit_cleanup 5
   fi
else
   # good transfer, now see if the file actually got written to the HMC
   if [ -f /usr/local/hsc_install.images/$FILE ]
   then
      chmod 777 /usr/local/hsc_install.images/$FILE
      
      # unpack the file
      rpm -i /usr/local/hsc_install.images/$FILE --force 2> /tmp/rpm.log
      RPMRC=$?
      chmod 777 /tmp/rpm.log
              
      if [ $RPMRC -eq 0 ] ; then
         exit_cleanup 0
      else 
         exit_cleanup 7
      fi
   else
      exit_cleanup 6
   fi
fi
exit_cleanup 0

